from django import forms
from django.core import validators
from app.form import FormMixin
from utils import mcache
from .models import User

class LoginForm(forms.Form, FormMixin):
    # 自定义验证器 1[3-9][0-9]\d{8} 111111111111  error_message
    telephone = forms.CharField(validators=[validators.RegexValidator(r'1[3-9][0-9]\d{8}', message="手机号格式有误")],
                                error_messages={"required": "号码不能为空"})
    # telephone = forms.CharField(max_length=11, min_length=11, error_messages={"min_length":"小于11位"})
    password = forms.CharField(min_length=6, max_length=20,
                               error_messages={"required": "密码框不能为空", "min_length": "长度不能低于6位",
                                               "max_length": "长度不能超过20位"})
    # 如果说 true false
    remember = forms.BooleanField(required=False)
    # 只有  Login


# 多继承 FormMixin get_error
class RegisterForm(forms.Form, FormMixin):
    # 自定义验证器 1[3-9][0-9]\d{8} 111111111111  error_message
    telephone = forms.CharField(validators=[validators.RegexValidator(r'1[3-9][0-9]\d{8}', message="手机号格式有误")],
                                error_messages={"required": "号码不能为空"})
    password = forms.CharField(min_length=6, max_length=20,
                               error_messages={"required": "密码框不能为空", "min_length": "长度不能低于6位",
                                               "max_length": "长度不能超过20位"})
    username = forms.CharField(max_length=20, min_length=1, error_messages={"required": "用户名不能为空"})
    sms_captcha = forms.CharField(max_length=4, min_length=4, error_messages={"required": "短信验证码不能为空"})
    password_repeat = forms.CharField(min_length=6, max_length=20,
                                      error_messages={"required": "密码框不能为空", "min_length": "长度不能低于6位",
                                                      "max_length": "长度不能超过20位"})
    graph_captcha = forms.CharField(max_length=4, min_length=4, error_messages={"required": "短信验证码不能为空"})

    # 自定义验证 self ==>  form  防止用到 注册  验证码不用保存导数据库
    def valid_data(self):
        # 验证 短信验证码/ 图形验证/确认密码
        sms_captcha = self.cleaned_data.get("sms_captcha")
        server_sms_captcha = mcache.get_key("sms_captcha")
        if not server_sms_captcha:
            return self.add_error("sms_captcha", "短信验证码已过期")
        if sms_captcha.lower() != server_sms_captcha.lower():
            return self.add_error("sms_captcha", "短信验证码不正确")

        password = self.cleaned_data.get("password")
        password_repeat = self.cleaned_data.get("password_repeat")
        if password != password_repeat:
            return self.add_error("password", "两次密码不一致")

        graph_captcha= self.cleaned_data.get("graph_captcha")
        server_graph_captcha= mcache.get_key("graph_captcha")
        if not server_graph_captcha:
            return self.add_error("graph_captcha", "图形验证码已过期")
        if graph_captcha.lower() != server_graph_captcha.lower():
            return self.add_error("sms_captcha", "短信验证码不正确")

        # 用户是否存在
        telephone = self.cleaned_data.get("telephone")
        # exists  True/False
        user = User.objects.filter(telephone=telephone).exists()
        if user:
            return self.add_error("telephone", "该用户已经注册")

        # 不返会True  只有全部验证过了 才会返回True
        return True